<?php
/* Copyright (C) 2007 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2010 Jean-Francois FERRY  <jfefe@aternatik.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/**
 *   	\file       ovhsms/sms.php
 *		\ingroup    ovhsms
 *		\brief
 *    \version    $Id: sms.php,v 1.8 2009/03/09 11:28:12 eldy Exp $
 */

$res=@include("../../main.inc.php");									// For "custom" directory
if (! $res) $res=@include("../main.inc.php");						// For root directory

if (!defined('DOL_CLASS_PATH')) define('DOL_CLASS_PATH', null);

require_once(DOL_DOCUMENT_ROOT.'/ovhsms/'.DOL_CLASS_PATH.'ovhsms.class.php');
require_once(DOL_DOCUMENT_ROOT.'/contact/'.DOL_CLASS_PATH.'contact.class.php');
require_once(DOL_DOCUMENT_ROOT.'/lib/company.lib.php');
require_once(DOL_DOCUMENT_ROOT.'/societe/'.DOL_CLASS_PATH.'societe.class.php');

// Load traductions files requiredby by page
$langs->load("companies");
$langs->load("@ovhsms");

// Get parameters
$socid = isset($_REQUEST["id"])?$_REQUEST["id"]:'';

// Protection if external user
if ($user->societe_id > 0)
{
	accessforbidden();
}



/*******************************************************************
* ACTIONS
*
********************************************************************/
/*
* Associer un compte OVH SMS à un tiers (non utilisé)
*
if ($_REQUEST["action"] == 'add_account')
{
	$sms=new OvhSms($db);
	$sms->account=$_POST["ovh_account"];
	$sms->fk_soc=$_POST["fk_soc"];
	$result=$sms->create($user);
	if ($result > 0)
	{
		// Creation OK
      $mesg = "<div class=\"ok\">Compte SMS associé !</div>";
	}
   	else
	{
		// Creation KO
		$mesg=$sms->error;
	}
}

*/







/***************************************************
* PAGE
*
* Put here all code to build page
****************************************************/

$morejs=array("/ovhsms/js/ovhsms.js");
llxHeader('','OvhSms','','','','',$morejs,'',0,0);



$form=new Form($db);








/*
// Cherche un comte SMS associé au client
 * dans le but de diviser le compte principal OVH (non utilisé)
$account = $sms->fetch_account($fk_soc);

if(!$account)
{
   print '<p class="error">Pas de compte SMS associé !</p>';
   print $sms->error;
   if($user->admin)
   {

      print '<p>Pour permettre à un tiers d\'utiliser l\'envoi de SMS, il est préférable de lui associer un compte distinct pour suivre sa consommation et ainsi la facturation.</p>';
      // Association compte sms OVH / client
      print '<form action="" method="POST">';
      print '<input type="hidden" name="fk_soc" id="fk_soc" value="'.$fk_soc.'" />';
      print '<input type="hidden" name="action" id="action" value="add_account" />';
      print '<table width="75%" class="liste">
      <tr>

      <th class="liste_titre">'.$langs->trans("Account").'</th>
      <th class="liste_titre">'.$langs->trans("Customer").'</th>';
      print '</tr>';
      print '<tr><td>Associer au compte</td><td>';
      print $sms->printListAccount();
      print '</td></td>';
      print '<tr><td></td><td><input type="submit" class="bouton" value="Associer"/></td>';
      print '</table></form>';
   }


}

else
{
*/



		/* Envoi d'un SMS */
	if ($_REQUEST["action"] == 'smsenvoi')
	{

	   $sms = new OvhSms($db);
	   $sms->expe = $_POST['expe'];
	   $sms->dest = $_POST['dest'];
	   $sms->message = $_POST['message'];
	   $sms->deferred = $_POST['deferred'];
	   $resultsend = $sms->SmsSend();
	   if ($resultsend > 0)
	   {

	      $mesg = '<p class="ok">Message correctement envoyé à '.$sms->dest.' sous la référence '.$resultsend.'</p>';
	   }

	  else $mesg = '<p class="error">'.$sms->error.'</p>';
	}


if ($socid)
{

   $sms = new OvhSms($db);
   /*
   * Creation de l'objet client/fournisseur correspondant au socid
   */

   $soc = new Societe($db);
   $result = $soc->fetch($socid);



   /*
   * Affichage onglets
   */
   $head = societe_prepare_head($soc);
   dol_fiche_head($head, 'tabOVHSMS', $langs->trans("ThirdParty"),0,'company');


   if ($mesg) print $mesg."<br>";


   	print '<table width="100%" class="notopnoleftnoright">';
	print '<tr><td valign="top" class="notopnoleft">';



	// Liste des expéditeurs autorisés
   $resultsender = $sms->SmsSenderList($account);

	print_titre($langs->trans("OvhSmsSend"));


   print '<form method="post" action="">';
   print '<input type="hidden" name="action" value="smsenvoi">';
   print '<table  class="border" width="100%">';
   print '<tr>
   <td>Expediteur</td>
   <td><select name="expe" id="valid" size="1">';

   $i=0;
   while($resultsender[$i]){
      print '<option value="'.$resultsender[$i]->number.'">'.$resultsender[$i]->number.'</option>';
      $i++;
   }
   print '</select></td>';

   print '</tr>
   <tr>
      <td>'.$langs->trans("OvhSmsDestinataire").'</td>
      <td><input type="text" name="dest" id="dest" size="15" value="+"><br />'.$langs->trans("OvhSmsInfoNumero").'</td>
   </tr>
   <tr>
      <td>'.$langs->trans("OvhSmsLabelTextMessage").'</td>
      <td>
         <textarea  cols="30" name="message" id="message" rows="4" onkeyup="limitChars(this, 160, \'charlimitinfo\')"> </textarea>
         <div id="charlimitinfo" style="color:#aa3333; font-size:12px; font-family:vardana" ><span >160</span> '.$langs->trans("OvhSmsInfoCharMax").'</div>
      </td>
   </tr>
   <tr>
      <td>'.$langs->trans("OvhSmsDifferSend").'</td>
      <td> <input name="deferred" id="deferred" size="4" value="0"> ('.$langs->trans("OvhSmsDelay").')</td>
   </tr>
   <tr>
      <td>'.$langs->trans("OvhSmsType").'</td>
      <td>
         <select name="class" id="valid" size="1">
            <option value="0">Flash</option>
            <option value="1" selected="selected">Standard</option>
            <option value="2">SIM</option>
            <option value="3">ToolKit</option>
         </select>
      </td>
   </tr>
   <tr>
      <td>'.$langs->trans("OvhSmsPriority").'</td>
      <td>
         <select name="class" id="valid" size="1">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3" selected="selected">3</option>
         </select>
      </td>
   </tr>
   <tr>
      <td></td>
      <td>
         <input type="submit" name="Submit" value="'.$langs->trans("OvhSmsSend").'" class="button">
      </td>

   </table>

   </form>
   ';





	print "</td>\n";


	print '<td valign="top" width="50%" class="notopnoleftnoright">';



 	 $sms->show_sms_contacts($conf,$langs,$db,$soc);



   print '</div>';
   print "</td></tr>";
	print "</table></div>\n";




}




// }





// End of page
$db->close();
llxFooter('');
?>
